#include "StdAfx.h"
#include "ConfigMgr.h"


CConfigMgr* CConfigMgr::m_instance;

CConfigMgr::CConfigMgr(void) : m_initError(FALSE)
{
	m_spDoc.CreateInstance(__uuidof(MSXML2::DOMDocument60));
	VARIANT_BOOL ret = m_spDoc->load(L"app.config");

	if (!ret)
	{
		m_initError = TRUE;
		SetLastErrorString(L"CConfigMgr::CConfigMgr: app.config no exists or incorrect!");
	}
	else
	{
		m_spRoot = m_spDoc->documentElement;
	}
}


CConfigMgr::~CConfigMgr(void)
{
	
}


HRESULT CConfigMgr::GetConnectionString(BSTR name, _bstr_t& value)
{
	if (m_initError)
	{
		return E_FAIL;
	}
	
	long len;
	MSXML2::IXMLDOMElementPtr spConnStringsNode = m_spRoot->selectSingleNode(L"connectionStrings");
	MSXML2::IXMLDOMNodeListPtr spConnStringList = spConnStringsNode->GetchildNodes();
	spConnStringList->get_length(&len);
	for(long i=0; i<len; i++)
	{
		MSXML2::IXMLDOMNodePtr spNode;
		spConnStringList->get_item(i, &spNode);
		MSXML2::IXMLDOMElementPtr spEl = spNode;
		_bstr_t nameVal = spEl->getAttribute(L"name").bstrVal;
		if (nameVal == _bstr_t(name))
		{
			value = spEl->getAttribute(L"connectionString").bstrVal;
			return S_OK;
		}
		
	}
	SetLastErrorString(L"CConfigMgr::GetConnectionString: ConnectionString no exists!");
	return E_FAIL;
}


HRESULT CConfigMgr::Init(void)
{

	return E_NOTIMPL;
}
